<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
  <link rel="stylesheet" th:href="@{/}+'js/citypicker/city-picker.css'" media="all">
  <style type="text/css">
	.layui-form .layui-form-item{
		width:90%;
	}
	.layui-input-inline{
		width:200px!important
	}
  </style>
</head>
<body style="margin-top: 10px">
	<form lay-filter="myform" id="myform" class="layui-form">
		<input type="hidden" name="id">
		<input type="hidden" name="state" value="1">
		<input type="hidden" name="code">
		<input type="hidden" name="logoUrl">
		<div class="layui-row layui-col-space8">
			<div class="layui-col-md10">
				<div class="layui-form-item">
					<label class="layui-form-label">机构全称</label>
					<div class="layui-input-inline">
					<input type="text" name="name" lay-verify="required" autocomplete="off" class="layui-input">
					</div>
					<label class="layui-form-label">机构简称</label>
					<div class="layui-input-inline">
					<input type="text" name="shortName" lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">上级机构</label>
					<div class="layui-input-inline">
					<ul id="parentId" class="dtree" data-id="0" ></ul>
					</div>
					<label class="layui-form-label">排序</label>
					<div class="layui-input-inline">
					<input type="number" name="orderno" lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">机构编号</label>
					<div class="layui-input-inline">
					<input type="text" name="orgCode" readonly="readonly" autocomplete="off" placeholder="系统自动生成" class="layui-input">
					</div>
					<label class="layui-form-label">机构类型</label>
					<div class="layui-input-inline">
						<select name="orgType" lay-filter="orgType"></select>
					</div>
					
				</div>
			</div>
			<div class="layui-col-md2">
				<div class="layadmin-homepage-panel layadmin-homepage-shadow">
        <div class="layui-card text-center">
          <div class="layui-card-body">
            <div class="layadmin-homepage-pad-ver">
              <img class="layadmin-homepage-pad-img" src="layuiadmin/style/res/template/portrait.png" width="96" height="96">
            </div>
            <h4 class="layadmin-homepage-font">公司logo</h4>
            <button class="layui-btn layui-btn-primary" id='uploadLogo'>上传</button>
          </div>
        </div>
      </div>
    </div>
			</div>
		</div>
		<button id="submitBtn" class="layui-btn" style="display:none" lay-submit lay-filter="submitme">保存</button>
	</form>
	<button id="uploadbtn" style="display:none">上传</button>
 <script th:inline="javascript">
/*<![CDATA[*/
var userId=/*[[${session.me.id}]]*/     
var orgid=/*[[${id}]]*/     
var pid=/*[[${pid}]]*/     
/*]]>*/
</script>
<link rel="stylesheet" href="layuiadmin/plugins/dtree/dtree/dtree.css">
<link rel="stylesheet" href="layuiadmin/plugins/dtree/dtree/font/dtreefont.css">
<script th:inline="none">
layui.extend({
	tableSelect: '{/}layuiadmin/plugins/tableSelect' 
});
layui.extend({
    dtree: '{/}layuiadmin/plugins/dtree/dist/dtree' 
});
layui.use(['form','table','laydate','tableSelect','dtree','upload'], function(){
	var form = layui.form
	,cityPicker = layui.citypicker
	,upload = layui.upload
	,tableSelect = layui.tableSelect
	,dtree = layui.dtree
	,table = layui.table
	,laydate = layui.laydate;
	$("#uploadLogo").on("click",function(){
		$('#uploadbtn').click();
		return false;
    })
	upload.render({
		elem: '#uploadbtn'
		,url: 'org/uploadLogo'
		,size: 4000 
		,before: function(obj){
			this.data = {orgId:orgid}
		}
		,done: function(res){
			$("input[name=logoUrl]").val(res.data.src)
			$('.layadmin-homepage-pad-img').attr("src",res.data.src+"?v="+new Date());
		}
	});
	var orgTree = dtree.renderSelect({
		elem: "#parentId",
		url: "org/getDTree",
		checkbar: false ,
		menubar: true,
		selectTips: '选择组织机构'
		,done:function(){
			if(orgid){
				var entity = getEntityInfo(orgid,"org/getById");
				if(entity.parentId){
					dtree.dataInit("parentId", entity.parentId);
					dtree.selectVal("parentId",entity.parentId);
				}
			}else if(pid) {
				dtree.dataInit("parentId", pid);
				dtree.selectVal("parentId",pid);
			}
		}
	})
	tableSelect.render({
		elem: '#managerName',	
		checkedKey: 'id' ,
		searchKey: 'name',	
		searchPlaceholder: '姓名/手机号',	
		table: {	
			url: 'user/getPageList' ,
			cols: [[
				 {type: 'checkbox'}
				,{field: 'id', width: 150, title: 'ID',hide:true}
				,{field: 'name', width: 130, title: '姓名'}
				,{field: 'mobileno', title: '手机号',width:150}
			]]
		},done: function (elem, data) {
			var v = data.data;
			var ids = [],names=[];
			for(var i=0;i<v.length;i++){
				ids.push(v[i].id);
				names.push(v[i].name);
			}
			if(v.length>0){
				$("#managerId").val(ids.join(","))
				$("#managerName").val(names.join(","));
			}
		}
	})
	tableSelect.render({
		elem: '#superintendentName',	
		checkedKey: 'id' ,
		searchKey: 'name',	
		searchPlaceholder: '姓名/手机号',	
		table: {	
			url: 'user/getPageList' ,
			cols: [[
				 {type: 'checkbox'}
				,{field: 'id', width: 150, title: 'ID',hide:true}
				,{field: 'name', width: 130, title: '用户姓名'}
				,{field: 'mobileno', title: '手机号',width:150}
			]]
		},done: function (elem, data) {
			var v = data.data;
			if(v.length==1){
				$("#superintendentId").val(v[0].id)
				$("#superintendentName").val(v[0].name)
			}
		}
	})
	if(!pid){
		createSelect("org-type","orgType",form,"head");
	}else{
		var parentEntity = getEntityInfo(pid,"org/getById");
		createSelect("org-type","orgType",form,"department");
		
	}
	function loadData(){
		var entity = getEntityInfo(orgid,"org/getById");
		if(entity.orgType!=='head'){
			$('.layui-col-md2').remove();
		}
		setTimeout(function(){
			form.val("myform",entity);
			var parentId = $("input[name=parentId]").val();
			if(entity.logoUrl)
				$('.layadmin-homepage-pad-img').attr("src",entity.logoUrl);
			form.render(); 
		 },100)
	 }
	  
	 if(orgid){
		 loadData();
	 }else{
		 $('.layui-col-md2').remove();
	 }
	form.on('submit(submitme)', function(data){
		var values = {};
		$(":input").each(function(i,v){
			values[v.name] = v.value;
		})
        values.parentId = values.parentId_select_nodeId;
		$.ajax({
			url: 'org/save',
			data: values,
			type:'POST',
			dataType:'JSON',
			success:function(ret){
				if(ret.code==1){
					return layer.msg(ret.msg,{icon:0});
				}
				var win = parent;
				win.refreshNode();
				win.loadGrid();
				layer.msg("保存成功");
				var index = parent.layer.getFrameIndex(window.name); 
				parent.layer.close(index); 
			}
		})
		return false; 
	});
	  
})

</script>
</body>
</html>